package com.qk.management.oss;

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.Protocol;
import com.aliyun.oss.common.comm.SignVersion;

import com.aliyun.oss.model.PutObjectRequest;

import java.io.File;

public class AliYunOssTest01 {
        public static void main(String[] args) throws Exception {
            // 创建 ClientBuilderConfiguration 实例，用于配置 OSS 客户端参数
            ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
            // 设置签名算法版本为 V4
            clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
            // 设置使用 HTTPS 协议访问 OSS，保证传输安全性
            clientBuilderConfiguration.setProtocol(Protocol.HTTPS);

            // 创建 OSS 客户端实例
            OSS ossClient = OSSClientBuilder.create()
                    // 以华东1（杭州）地域的外网访问域名为例，Endpoint填写为oss-cn-hangzhou.aliyuncs.com
                    .endpoint("oss-cn-hangzhou.aliyuncs.com")
                    // 从环境变量中获取访问凭证（需提前配置 OSS_ACCESS_KEY_ID 和 OSS_ACCESS_KEY_SECRET）
                    .credentialsProvider(CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider())
                    // 设置客户端配置
                    .clientConfiguration(clientBuilderConfiguration)
                    // 以华东1（杭州）地域为例，Region填写为cn-hangzhou
                    .region("cn-hangzhou")
                    .build();

            try {
                //文件上传
                PutObjectRequest putObjectRequest = new PutObjectRequest("qk-data", "12.png",new File("C:\\Users\\chouh\\Pictures\\小箱.png"));
                ossClient.putObject(putObjectRequest);

            } finally {
                // 当OSSClient实例不再使用时，调用shutdown方法以释放资源
                ossClient.shutdown();
            }
        }
    }
